<?php

namespace cccmvc;

class View
{
    protected static array $config;
    protected static \Smarty $view;

    public function __construct($config)
    {
        self::$config = $config;
        $smarty = new \Smarty();
        $smarty->setCompileDir($config['compile_dir'])
            ->setTemplateDir($config['template_dir'] . '/')
            ->setCacheDir($config['cache_dir']);
        self::$view = $smarty;
    }

    public static function render(string $tpl, array $data = [])
    {
        $smarty = self::$view;

        foreach ($data as $key => $val) {
            $smarty->assign($key, $val);
        }

        $tplPath = $tpl . '.tpl.php';

        try {
            $smarty->display($tplPath);
        } catch (\Exception $e) {
            return "err";
        }
    }


}